<template>
  <div>
    <div class="header">
      <!-- 搜索 -->
      <div class="head_Search">
        <icon
          type="search"
          size="20">
        </icon>
        <input type="text" v-model="keyWord" @confirm="qeuryGoods">
      </div>
    </div>
    <!-- 过滤菜单 -->
    <ul class="filter-menu">
      <li :class="{'active': activeIndex === index}" @click="activeIndex = index" v-for="(item, index) in menu" :key="index">{{ item }}</li>
    </ul>
    <!-- 商品列表 -->
    <ul class="goods-list">
      <li v-for="(item, index) in goodsList" :key="index">
        <img :src="item.goods_big_logo" alt="">
        <div class="right">
          <p class="goods-name text-align">{{ item.goods_name }}</p>
          <p class="goods-price">￥<span>{{ item.goods_price }}</span>.00</p>
        </div>
      </li>
    </ul>
    <p class="btm-text" v-if="isLastPage">已经到达底部了</p>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menu: ['综合', '销量', '价格'],
      activeIndex: 0,
      keyWord: '',
      goodsList: [],
      pagenum: 1,
      pagesize: 6,
      isRequest: false, // 是否在请求中
      isLastPage: false // 是否已经请求到最后一页
    }
  },
  onLoad (options) {
    this.keyWord = options.query
    this.qeuryGoods()
  },
  onPullDownRefresh () {
    this.pagenum = 1
    this.isLastPage = false
    this.goodsList = []
    this.qeuryGoods()
    wx.stopPullDownRefresh()
  },
  onReachBottom () {
    if (!this.isLastPage) {
      this.pagenum++
    }
    this.qeuryGoods()
  },
  methods: {
    async qeuryGoods () {
      // 如果请求中，就不能继续发请求请求数据
      if (this.isRequest || this.isLastPage) {
        return
      }
      // 发送请求前，设置isRequest为true
      this.isRequest = true
      let data = await this.$request({
        url: '/api/public/v1/goods/search',
        data: {
          query: this.keyWord,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      // 请求完后，isRequest为false
      this.isRequest = false
      this.goodsList = [...this.goodsList, ...data.goods]
      // 在这里可以判断是否已经是最后一页
      if (this.goodsList.length === data.total) {
        this.isLastPage = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.header {
  height: 120rpx;
  background-color: #eee;
  padding: 16rpx;
  display: flex;
  align-items: center;
  .head_Search {
    flex: 1;
    height: 60rpx;
    position: relative;
    icon {
      position: absolute;
      left: 10rpx;
      top: 8rpx;
    }
    input {
      width: 100%;
      height: 100%;
      border-radius: 8rpx;
      background-color: #fff;
      box-sizing: border-box;
      padding-left: 60rpx;
    }
  }
}

.filter-menu {
  height: 100rpx;
  border-bottom: solid 2rpx #ddd;
  display: flex;
  box-sizing: border-box;
  justify-content: space-evenly;
  align-items: center;
    li.active {
      color: #eb4d5b;
    }
}

.goods-list {
  li {
    height: 260rpx;
    box-sizing: border-box;
    border-top: 1rpx solid #ddd;
    display: flex;
    align-items: center;
    padding: 0 20rpx;
    >img {
      width: 200rpx;
      height: 200rpx;
    }
    .right {
      flex: 1;
      height: 200rpx;
      display: flex;
      flex-direction: column;
      margin-left: 24rpx;
      justify-content: space-between;
    }
    .goods-price {
      font-size: 24rpx;
      color: #eb4450;
      span {
        font-size: 32rpx;
      }
    }
  }
}

.btm-text {
  font-size: 40rpx;
  text-align: center;
}
</style>